//The purpose of this class is to allow the user to choose a historical date.
package com.mckillion.icare;

//Import the java classes required
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.CalendarView.OnDateChangeListener;
import com.mckillion.icare.R;

//The class extends activity which allows the Android features to be imported.
//It also extends on date change to allow the calendar to become interactive.
public class IntakeHistory extends Activity implements OnDateChangeListener {
	//Declare the variables required for this class.
	//private variables are used to promote consistency.
	private ImageButton backButton, homeButton, settingsButton;
	private CalendarView chooseDate;
	private ImageView infoButton;
	private TextView infoText;
	
	//overwrite the onCreate method so that variables are instantiated on opening.
	public void onCreate(Bundle savedInstanceState) {
        //Call to the super class.
		super.onCreate(savedInstanceState);
        
		//Remove title and notification bars to give it an android feel.
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        
		//Set the content layout to the main_screen XML file.
        setContentView(R.layout.calarie_history);
        
        //Make the calendar view object match the object in the layout file
        chooseDate = (CalendarView)findViewById(R.id.choose_date);
        
        //Create a listener that reacts to a date being chosen.
        chooseDate.setOnDateChangeListener(this);
                
    	//make the objects match the objects in the layout file.
        backButton = (ImageButton) findViewById(R.id.back_button);
        homeButton = (ImageButton) findViewById(R.id.home_button);
        settingsButton = (ImageButton) findViewById(R.id.settings_button);
    	infoButton=(ImageView)findViewById(R.id.imageView4);
    	
    	//Set a listener for the information button
        infoButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				//Use a layout inflater to open a pop-up window with the informational text.
				LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
				View popupView = layoutInflater.inflate(R.layout.info_popup, null);  
			    final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT,  LayoutParams.WRAP_CONTENT);
			    
			    infoText = (TextView) popupWindow.getContentView().findViewById(R.id.info_text);
			    infoText.setText("This is the intake history home screen." + "\n" + "\n" +
			    					"Choose a date on the calendar to view that days intake." + "\n" + "\n" +
			    					"Press dismiss to close this message.");
			    
			    //create a dismiss button to close the pop-up window.
			    Button dismiss = (Button)popupView.findViewById(R.id.dismiss_button);
	            dismiss.setOnClickListener(new Button.OnClickListener(){
	            	@Override
	            	public void onClick(View v) {
	            		popupWindow.dismiss();
	            	}});
	               
	            //Put the pop-up window in the centre of the screen
	            popupWindow.showAtLocation(infoButton, Gravity.CENTER, 0, 0);
	   }});
        
        //Set a listener for the home button which starts the HomeScreen class
        homeButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(IntakeHistory.this, HomeScreen.class);
				startActivity(i);
			}
		});
        
        //Set a listener for the inactivity button
        backButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(IntakeHistory.this, ComputerScreen.class);
				startActivity(i);
			}
		});
        
        //Set a listener for the back button which starts the ComputerScreen class
        settingsButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(IntakeHistory.this, Settings.class);
				startActivity(i);
			}
		});
	}
	
	@Override
	public void onBackPressed() {
		//handle the back button being pressed
		Intent i = new Intent(IntakeHistory.this, ComputerScreen.class);
		startActivity(i);
	}

	//Create a method which opens the date chosen on the Calendar view.
	@Override
	public void onSelectedDayChange(CalendarView arg0, int arg1, int arg2,
			int arg3) {
		//Open the intake diary screen with the selected date.
		Intent i = new Intent(this, IntakeDiary.class);
		String s = arg3 + "-" + (arg2 + 1) + "-" + arg1;
		i.putExtra("date", s);
		startActivity(i);		
	}
}
